package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.GroupTalkBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.lsn.SendMsgCenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/8 17:45
 */
public class WechatChatAddRedpkgReceiveActivity extends BaseActivity {


    @BindView(R2.id.tv_sender)
    TextView     mTvSender;
    @BindView(R2.id.iv_avatar)
    ImageView    mIvAvatar;
    @BindView(R2.id.rv)
    RecyclerView mRv;
    @BindView(R2.id.tv_receiver)
    TextView     mTvReceiver;
    @BindView(R2.id.iv_receiver)
    ImageView    mIvReceiver;
    @BindView(R2.id.rv2)
    RecyclerView mRv2;
    @BindView(R2.id.switch_is_finish)
    Switch       mSwitchIsFinish;

    private boolean                      isReceiveFinish;//红包是否已经领取完毕
    private GroupTalkBean                mInitGroupTalk;
    private ArrayList<WechatContactBean> list;
    private WechatContactBean            mSender, mReceiver;

    public WechatChatAddRedpkgReceiveActivity() {
        super(R.layout.wechat_activity_chat_add_redpacket_receive);
    }

    public static void start(Activity act, GroupTalkBean groupTalkBean) {
        Intent it = new Intent(act, WechatChatAddRedpkgReceiveActivity.class);
        it.putExtra("GroupTalkBean", groupTalkBean);
        act.startActivity(it);
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        //添加数据（me）
        WechatContactBean me = WechatContactBean.instanceNewContactMe(this);
        onSenderChange(me, true);//默认选中发送者为自己
        onSenderChange(me, false);//默认选中接收者为自己
        list.add(me);
        //添加数据（other）
        mInitGroupTalk = (GroupTalkBean) getIntent().getSerializableExtra("GroupTalkBean");
        list.addAll(mInitGroupTalk.getGroupMembersList());
        //发送者选择adapter
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mRv.setAdapter(new ContactAdapter(R.layout.wechat_select_sender_contact_item, list, true));
        //接收者选择adapter
        mRv2.setLayoutManager(new LinearLayoutManager(this));
        mRv2.setAdapter(new ContactAdapter(R.layout.wechat_select_sender_contact_item, list, false));
        //监听
        mSwitchIsFinish.setOnCheckedChangeListener((buttonView, isChecked) -> isReceiveFinish = isChecked);
    }

    @OnClick({R2.id.ll_sender, R2.id.ll_receiver, R2.id.btn_save})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.ll_sender) {
            if (mRv.getVisibility() == View.VISIBLE) {
                mRv.setVisibility(View.GONE);
            } else {
                mRv.setVisibility(View.VISIBLE);
            }
        } else if (id == R.id.ll_receiver) {
            if (mRv2.getVisibility() == View.VISIBLE) {
                mRv2.setVisibility(View.GONE);
            } else {
                mRv2.setVisibility(View.VISIBLE);
            }
        } else if (id == R.id.btn_save) {
            SendMsgCenter.sendRedpkgReceive(new SendMsgCenter.SendMsgListener() {
                @Override
                public void onSendSuc() {
                    finish();
                }

                @Override
                public void onSendError(String errorMsg) {

                }
            }, mInitGroupTalk.getId(), mSender, mReceiver,isReceiveFinish);
        }
    }

    public class ContactAdapter extends BaseQuickAdapter<WechatContactBean, BaseViewHolder> {
        private boolean isSender;

        public ContactAdapter(int layoutResId, List data, boolean isSender) {
            super(layoutResId, data);
            this.isSender = isSender;
        }

        @Override
        protected void convert(BaseViewHolder helper, final WechatContactBean vo) {
            helper.setText(R.id.name, vo.getName());
            WechatContactBean.setupAvatarIntoImageView(vo, helper.getView(R.id.iv_avatar), 12);
            helper.setChecked(R.id.rb, vo.isSelected());
            helper.getView(R.id.vg).setOnClickListener(v -> {
                for (WechatContactBean item : ContactAdapter.this.getData()) {
                    item.setSelected(false);
                }
                notifyDataSetChanged();
                onSenderChange(vo, isSender);
            });
        }
    }

    //选中后改变布局显示
    private void onSenderChange(WechatContactBean vo, boolean isSender) {
        vo.setSelected(true);
        if (isSender) {
            mSender = vo;
            mRv.setVisibility(View.GONE);
            WechatContactBean.setupAvatarIntoImageView(vo, mIvAvatar, 12);
            mTvSender.setText(vo.getName());
        } else {
            mReceiver = vo;
            mRv2.setVisibility(View.GONE);
            WechatContactBean.setupAvatarIntoImageView(vo, mIvReceiver, 12);
            mTvReceiver.setText(vo.getName());
        }
    }
}
